package cn.uniquesoft.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.uniquesoft.controller.base.BaseController;
import cn.uniquesoft.service.IRoleService;
import cn.uniquesoft.service.base.IBaseService;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.RoleVO;

//系统角色表 控制器
@Controller
@SuppressWarnings("unused")
@RequestMapping(value = "/role")
public class RoleController extends BaseController<RoleVO> {

	private IRoleService roleService;

	public RoleController(IRoleService roleService) {
		super();
		this.roleService = roleService;
		this.baseService = (IBaseService<RoleVO>) roleService;
		this.indexName = "role/index";
	}
	
	
	// 获取角色ID和菜单ID，插入sys_role_menu表中
	@RequestMapping(value="/insertRoleMenu")
	@ResponseBody
	public AnswerVO insertRoleMenu(RoleVO item){
		AnswerVO answerVO = new AnswerVO();
		// 获取角色ID和菜单ID
		if (null == item) {
			answerVO.setSuccess(false);
			answerVO.setMsg("操作失败！");
			return answerVO;
		}
		RoleVO rVO = new RoleVO();
		rVO.setIroleid(item.getIroleid());
		rVO.setImenuid(item.getImenuid());
		// 首先根据iroleid查找表中是否已经存在，如果存在则update,不存在则insert.
		item = this.roleService.findOneRoleMenuByiroleid(rVO);
		// 直接调用插入方法
		if (item==null) {
			this.roleService.insertOneRoleMenuByiroleid(rVO);
		}else if (rVO.getIroleid()!=0) {
			this.roleService.updateOneRoleMenuByiroleid(rVO);
		}
		answerVO.setSuccess(true);
		answerVO.setMsg("操作成功！");
		return answerVO;
		
	}
	
}